package com.live.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.live.adapter.LiveReplaySA;
import com.live.common.Constant;
import com.live.common.ImageDownloader;
import com.live.common.LiveCommon;
import com.live.task.LiveReplay;

public class LiveReplayViewActivity extends LiveBaseActivity {

	private String uId;
	private String userName;
	private Bundle bundle;
	private ListView newsReplayLV;
	private LiveReplaySA newsReplaySA;
	private List<Map<String, Object>> liveNewsReplayList = new ArrayList<Map<String, Object>>();
	private final ImageDownloader imageDownloader = new ImageDownloader();

	private View liveFoot;

	private int page = 0;

	private int lastIndex = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.live_news_replay);

		uId = store.getString("id", null);

		/* 获取Intent中的Bundle对象 */
		bundle = this.getIntent().getExtras();

		userName = bundle.getString("userId");

		// 设置头部信息
		LiveCommon.setHead(context, (LinearLayout) findViewById(R.id.liveHead), R.string.title_replay_view,
				R.string.topic_replay);

		// 设置头部按钮（回复）
		Button topicBtn = (Button) findViewById(R.id.headFuncBtn);
		topicBtn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 回复
				Intent intent = new Intent();
				intent.setClass(context, LiveReplayActivity.class);
				bundle.putString("matchId", bundle.getString("matchId"));
				bundle.putString("userName", userName);
				bundle.putString("atUserId", bundle.getString("uId"));
				bundle.putString("replayId", bundle.getString("id"));
				bundle.putString("userId", uId);
				intent.putExtras(bundle);
				startActivityForResult(intent, Constant.REQUEST_CODE);
			}
		});

		// 设置内容
		TextView content = (TextView) findViewById(R.id.content);
		if (bundle.getString("title") != null) {
			content.setText(Html.fromHtml("<font color='#2D86B0'>" + bundle.getString("userId") + "</font>"
					+ " <font color='#666666'>" + bundle.getString("content") + "</font> " + "<font color='#2D86B0'>"
					+ bundle.getString("title") + "</font>"));
		} else {
			content.setText(Html.fromHtml("<font color='#2D86B0'>" + bundle.getString("userId") + "</font> "
					+ bundle.getString("content")));
		}

		// 设置性别
		TextView userSex = (TextView) findViewById(R.id.userSex);
		if (Constant.USER_SEX_MAN.equals(bundle.getString("userSex"))) {
			userSex.setText("♂");
			userSex.setTextColor(0xff00CCFF);
		} else {
			userSex.setText("♀");
			userSex.setTextColor(0xffFF6699);
		}

		// 设置头像
		if (Constant.Y.equals(bundle.getString("userPic"))) {
			String imageUrl = Constant.SITE_URL + Constant.SITE_AVATAR_PATH + bundle.getString("uId") + ".jpg";
			imageDownloader.download(imageUrl, (ImageView) findViewById(R.id.userPic), bundle.getString("userPicVer"));
		}

		// 设置城市
		TextView userCity = (TextView) findViewById(R.id.userCity);
		userCity.setText(bundle.getString("userCity"));

		// 设置年龄
		TextView userAge = (TextView) findViewById(R.id.userAge);
		userAge.setText(bundle.getString("userAge"));

		// 添加“读取更多按钮”
		liveFoot = ((LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.live_foot, null, false);

		newsReplayLV = (ListView) findViewById(R.id.newsReplayList);
		newsReplaySA = new LiveReplaySA(context, liveNewsReplayList, R.layout.topic_list, new String[] {},
				new int[] {}, store.getString("userId", null));
		newsReplayLV.addFooterView(liveFoot);
		newsReplayLV.setAdapter(newsReplaySA);
		newsReplayLV.setOnItemClickListener(new OnItemClickListener() {

			// 从当前画面到比分明细画面跳转，并传值。
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				Map<String, Object> replay = liveNewsReplayList.get(arg2);

				// 不回复自己的回复
				if (!uId.equals((String) replay.get("uId"))) {
					// 回复
					Intent intent = new Intent();
					intent.setClass(context, LiveReplayActivity.class);
					bundle.putString("matchId", bundle.getString("matchId"));
					bundle.putString("userName", (String) replay.get("userId"));
					bundle.putString("atUserId", (String) replay.get("uId"));
					bundle.putString("replayId", bundle.getString("id"));
					bundle.putString("userId", uId);
					intent.putExtras(bundle);
					startActivityForResult(intent, Constant.REQUEST_CODE);
				}
			}
		});
		newsReplayLV.setOnScrollListener(new OnScrollListener() {

			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && lastIndex == newsReplaySA.getCount()) {
					page = page + 1;
					bundle.putInt("page", page);
					LiveReplay task = new LiveReplay(context, liveNewsReplayList, newsReplaySA, newsReplayLV, liveFoot);
					task.execute(bundle);
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

				// 计算最后可见条目的索引
				lastIndex = firstVisibleItem + visibleItemCount - 1;
			}
		});

		bundle.putInt("page", 0);
		LiveReplay task = new LiveReplay(context, liveNewsReplayList, newsReplaySA, newsReplayLV, liveFoot);
		task.execute(bundle);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == Constant.REQUEST_CODE) {
			if (resultCode != RESULT_CANCELED) {
				if (Constant.RELOAD.equals(data.getAction())) {
					bundle.putInt("page", 0);
					LiveReplay task = new LiveReplay(context, liveNewsReplayList, newsReplaySA, newsReplayLV, liveFoot);
					task.execute(bundle);
				}
			}
		}
	}
}